﻿//namespace
Type.registerNamespace("dnneurope.portalgroups");

dnneurope.portalgroups.PortalGroupViewModel = function (model) {
    var self = this;
    self.PortalGroupId = model.PortalGroupId;
    self.MasterPortal = model.MasterPortal;
    self.PortalGroupName = ko.observable(model.PortalGroupName);
    self.AuthenticationDomain = ko.observable(model.AuthenticationDomain);
    self.Portals = ko.observableArray(model.Portals);
    self.canBeDeleted = ko.computed(function () {
        return self.Portals().length == 0;
    });
};

dnneurope.portalgroups.ViewModel = function (model, localization, serviceClient) {
    var self = this;
    self.localize = localization;
    //data
    self.PortalGroups = ko.observableArray(
        ko.utils.arrayMap(
            model.PortalGroups, function (groupModel) {
                return new dnneurope.portalgroups.PortalGroupViewModel(groupModel);
            }));

    self.AvailablePortals = ko.observableArray(model.AvailablePortals);

    //selected items
    self.SelectedPortal = ko.observable();
    self.SelectedPortalGroup = ko.observable();
    self.SelectedPortals = ko.observableArray();

    var resetAllSelections = function () {
        self.SelectedPortalGroup(null);
        self.SelectedPortal(null);
        self.SelectedPortals([]);
    };

    //cache for rollbacks
    self.OriginalPortalGroup = ko.observable();
    self.OriginalAvailablePortals = ko.observableArray();

    //methods (events)
    self.addPortalGroup = function () {
        console.log(self.SelectedPortal().PortalName);
        var newGroup = new dnneurope.portalgroups.PortalGroupViewModel({
            PortalGroupId: -1,
            MasterPortal: ko.toJS(self.SelectedPortal()),
            PortalGroupName: self.localize.newPortalGroupPrefix + self.SelectedPortal().PortalName,
            Portals: []
        });
        self.PortalGroups.push(newGroup);
        self.AvailablePortals.remove(self.SelectedPortal());
        self.editPortalGroup(newGroup);
    };

    self.saveEdit = function (group) {
        serviceClient.save(ko.toJS(group)).done(function (id) {
            group.PortalGroupId = id;
            resetAllSelections();
        });
    };

    self.editPortalGroup = function (group) {
        resetAllSelections();
        self.SelectedPortalGroup(group);
        self.OriginalPortalGroup(ko.toJS(group));
        self.OriginalAvailablePortals(ko.toJS(self.AvailablePortals()));
    };

    self.addPortalToGroup = function (group) {
        group.Portals.push(self.SelectedPortal());
        self.AvailablePortals.remove(self.SelectedPortal());
    };

    self.removePortalsFromGroup = function (group) {
        ko.utils.arrayForEach(self.SelectedPortals(), function (portal) {
            self.AvailablePortals.push(portal);
            group.Portals.remove(portal);
        });
    };

    self.deletePortalGroup = function (group) {
        serviceClient.delete(group.PortalGroupId)
            .done(function () {
                ko.utils.arrayForEach(group.Portals(), function (portal) {
                    self.AvailablePortals.push(portal);
                });
                self.AvailablePortals.push(group.MasterPortal);
                self.PortalGroups.remove(group);
            });
    };

    self.cancelEdit = function (group) {
        var groupIsNew = group.PortalGroupId === -1;
        if (groupIsNew) {
            self.PortalGroups.remove(group);
        } else {
            group.PortalGroupName(self.OriginalPortalGroup().PortalGroupName);
            group.AuthenticationDomain(self.OriginalPortalGroup().AuthenticationDomain);
        }
        group.Portals(self.OriginalPortalGroup().Portals);
        self.AvailablePortals(self.OriginalAvailablePortals());
        resetAllSelections();
    };

    //visibility helpers
    self.showList = ko.computed(function () {
        return !self.SelectedPortalGroup();
    });
    self.showEditor = ko.computed(function () {
        return self.SelectedPortalGroup();
    });
    self.canCreateGroup = ko.computed(function () {
        return self.AvailablePortals() && self.AvailablePortals().length > 0;
    });
    self.canRemovePortalFromGroup = ko.computed(function () {
        return self.SelectedPortals() && self.SelectedPortals().length > 0;
    });
    //alias
    self.canAddPortalToGroup = self.canCreateGroup;

};